﻿using AzureVMDashboard.Model;
using System;
using System.Globalization;
using System.IO;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace AzureVMDashboard.UI
{
    public class UIHelper
    {
        private static Cursor currentCur = Mouse.OverrideCursor;

        public static void SetWaitCursor()
        {

            Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
        }

        public static void SetNormalCursor()
        {
            Mouse.OverrideCursor = currentCur;
        }
    }

    #region Converters
    public class VMStatusToTooltipConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            VMStatus status = (VMStatus)value;

            switch (status)
            {
                case VMStatus.Running:
                    return "Running";
                case VMStatus.Off:
                    return "Stopped";
                case VMStatus.Error:
                    return "Error";
                case VMStatus.Updating:
                    return "Updating status";
            }

            return String.Empty;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            // Do the conversion from visibility to bool
            return value;
        }
    }

    public class VMStatusToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            VMStatus status = (VMStatus)value;

                switch (status)
                {
                    case VMStatus.Running:
                        return new SolidColorBrush(Colors.Green);
                    case VMStatus.Off:
                        return new SolidColorBrush(Colors.Gray);
                    case VMStatus.Error:
                        return new SolidColorBrush(Colors.Red);
                    case VMStatus.Updating:
                        return new SolidColorBrush(Colors.Orange);             
            }

            return new SolidColorBrush(Colors.Black);
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            // Do the conversion from visibility to bool
            return value;
        }
    }

    public class InfoMessageToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return System.Windows.Visibility.Collapsed;

            InfoMessage status = (InfoMessage)value;

            switch (status.Level)
            {
                case LogType.Error:
                    return System.Windows.Visibility.Visible;
                default: 
                    return System.Windows.Visibility.Collapsed;
            }

            return new SolidColorBrush(Colors.Black);
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            // Do the conversion from visibility to bool
            return value;
        }
    }

    /// <summary>
    /// Show the element if the value is not NULL
    /// </summary>
    public class ExistToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return System.Windows.Visibility.Collapsed;

            else
                return System.Windows.Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    #endregion

    #region Validation Rules
    public class FileExistValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            string errorMessage = "File not exist";
            if(value == null)
                return new ValidationResult(false,errorMessage);

            String insertedValue = value as String;
            if(String.IsNullOrEmpty(insertedValue))
                return new ValidationResult(false, errorMessage);

            bool result = File.Exists(insertedValue);
            return result ? ValidationResult.ValidResult : new ValidationResult(false, errorMessage);
        }

    }
    #endregion 
}
